package cn.bby.tools.utils;

import cn.hutool.core.swing.clipboard.ImageSelection;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.util.List;

/**
 * @author ben
 * @since 2025/10/18 02:57
 */
public class ClipboardUtil {
    /**
     * 从剪贴板获取图片
     *
     * @return BufferedImage 剪贴板中的图片，如果没有则返回null
     */
    public static BufferedImage getImageFromClipboard() {
        try {
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Clipboard clipboard = toolkit.getSystemClipboard();
            Transferable transferable = clipboard.getContents(null);

            if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                Image image = (Image) transferable.getTransferData(DataFlavor.imageFlavor);
                if (image instanceof BufferedImage) {
                    return (BufferedImage) image;
                } else {
                    // 将Image转换为BufferedImage
                    BufferedImage bufferedImage = new BufferedImage(
                            image.getWidth(null),
                            image.getHeight(null),
                            BufferedImage.TYPE_INT_RGB
                    );
                    Graphics2D g = bufferedImage.createGraphics();
                    g.drawImage(image, 0, 0, null);
                    g.dispose();
                    return bufferedImage;
                }
            }
        } catch (Exception e) {
            System.out.println("Error getting image from clipboard: " + e.getMessage());
        }
        return null;
    }

    public static void setTextToClipboard(String text) {
        Toolkit.getDefaultToolkit().getSystemClipboard()
                .setContents(new StringSelection(text), null);
    }

    public static void setImageToClipboard(BufferedImage image) {
        Toolkit.getDefaultToolkit().getSystemClipboard()
                .setContents(new ImageSelection(image), null);
    }


    /**
     * 将列表复制为列选择
     */
    public static void copyAsColumnSelection(List<String> dataList) {
        String[][] data = new String[dataList.size()][1];
        for (int i = 0; i < dataList.size(); i++) {
            data[i][0] = dataList.get(i);
        }
        copyAsColumnSelection(data);
    }

    /**
     * 将二维数组复制为列选择
     * @param data 二维数组
     */
    public static void copyAsColumnSelection(String[][] data) {
        // 构建剪贴板字符串
        StringBuilder sb = new StringBuilder();
        for (String[] row : data) {
            for (int i = 0; i < row.length;  i++) {
                sb.append(row[i]  != null ? row[i] : "");
                if (i < row.length  - 1) {
                    sb.append("\t");  // 列分隔符
                }
            }
            sb.append("\n");  // 行分隔符
        }

        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection selection = new StringSelection(sb.toString());
        clipboard.setContents(selection,  null);
    }
}
